﻿using System.Linq.PageList;
using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class TaskSettingController : ApiV1BaseController
    {
        private readonly ITaskCenterSettingsApplication _taskCenterSettingsApp;

        public TaskSettingController(ITaskCenterSettingsApplication taskCenterSettingsApp)
        {
            _taskCenterSettingsApp = taskCenterSettingsApp;
        }

        /// <summary>
        /// 分页获取任务中心配置信息
        /// </summary>
        /// <param name="filterModel">分页筛选器</param>
        /// <param name="cancellationToken">取消操作的通知</param>
        /// <returns>返回任务中心配置</returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<TaskCenterSettingsDto>>> GetPageListAsync(TaskCenterSettingFilterDto filterModel, CancellationToken cancellationToken = default)
        {
            var filter = filterModel;
            var pageFilter = filter.ToPageFilter(filter);
            return (PageList<TaskCenterSettingsDto>)await _taskCenterSettingsApp.GetPageListAsync(pageFilter, cancellationToken);
        }

        /// <summary>
        /// 新增任务中心配置信息
        /// </summary>
        /// <param name="settingModel">配置信息</param>
        /// <param name="cancellationToken">取消操作的通知</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult> CreateTaskCenterSettingAsync(TaskCenterSettingsDto settingModel, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            await _taskCenterSettingsApp.CreateTaskCenterSettingAsync(settingModel, cancellationToken);
            return Ok();
        }

        /// <summary>
        /// 修改任务中心配置信息
        /// </summary>
        /// <param name="settingModel">配置信息</param>
        /// <param name="cancellationToken">取消操作的通知</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult> ModifiedTaskCenterSettingAsync(TaskCenterSettingsDto settingModel, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            await _taskCenterSettingsApp.ModifiedTaskCenterSettingAsync(settingModel, cancellationToken);
            return Ok();
        }

        /// <summary>
        /// 获取任务中心配置信息
        /// </summary>
        /// <param name="settingId">配置信息ID</param>
        /// <param name="cancellationToken">取消操作的通知</param>
        /// <returns>返回配置信息</returns>
        [HttpGet("[action]")]
        public async Task<ApiResult<TaskCenterSettingsDto>> GetTaskCenterSettingAsync(string settingId, CancellationToken cancellationToken = default)
        {
            return await _taskCenterSettingsApp.GetTaskCenterSettingAsync(settingId, cancellationToken);
        }

        /// <summary>
        /// 删除任务中心配置信息
        /// </summary>
        /// <param name="settingIds">配置主键集合</param>
        /// <param name="cancellationToken">取消操作的通知</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult> DeleteTaskCenterSettingAsync(List<string> settingIds, CancellationToken cancellationToken)
        {
            await _taskCenterSettingsApp.DeleteTaskCenterSettingAsync(settingIds, cancellationToken);
            return Ok();
        }

        /// <summary>
        /// 获取尚未配置的ActionKey信息
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<ApiResult<List<TaskCenterCodeDto>>> GetUnSettingActionKeysAsync(CancellationToken cancellationToken = default)
        {
            return await _taskCenterSettingsApp.GetUnSettingTaskCodesAsync(cancellationToken);
        }
    }
}
